﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VideoStream.CustomControl;
using VideoStream.Model;

namespace VideoStream.Windows
{
    public partial class Windows_MessageBox : VideoStream.Windows.WindowsBase
    {
        #region Delegate

        public delegate void MessageBoxResultHandler(DialogResult dlgResult);

        #endregion

        private string message;
        private MessageBoxButtons buttons;
        private MessageBoxIcon icon;
        private Point newLocation = Point.Empty;
        public MessageBoxResultHandler messageboxResult;

        public PictureButton BtnConfirm
        {
            get { return btn_Confirm; }
        }
        public PictureButton BtnCancel
        {
            get { return btn_Cancel; }
        }
        public Label LabMsg
        {
            get { return lab_Message; }
        }

        public Windows_MessageBox(string msg, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            InitializeComponent();
            this.message = msg;
            this.buttons = buttons;
            this.icon = icon;
            InitUI();
        }


        #region Override

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitEvent();
            UpdateUI();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (this.Owner != null)
                this.Owner.LocationChanged -= Owner_LocationChanged;
        }

        public override void InitEvent()
        {
            base.InitEvent();

            btn_Confirm.Click += Btn_Confirm_Click;
            btn_Cancel.Click += Btn_Cancel_Click;
        }

        public override void InitUI()
        {
            base.InitUI();

            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.CenterParent;
            //if (this.Owner == null)
            //    this.StartPosition = FormStartPosition.CenterScreen;
            SetConfirmText(TVString.STR_OK);
            SetCancelText(TVString.STR_Cancel);
            SetMessage(message);
            SetButtons(this.buttons);
        }

        public override void UpdateUI()
        {
            base.UpdateUI();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) Btn_Cancel_Click(null, null);

            if (keyData == Keys.Enter) Btn_Confirm_Click(null, null);

            return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion


        #region Control Event

        private void Btn_Confirm_Click(object sender, EventArgs e)
        {
            if (btn_Confirm.Enabled & btn_Confirm.Visible)
            {
                DialogResult = DialogResult.OK;
                messageboxResult?.Invoke(DialogResult);
                Close();
            }
        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            if (btn_Cancel.Enabled & btn_Cancel.Visible)
            {
                DialogResult = DialogResult.Cancel;
                messageboxResult?.Invoke(DialogResult);
                Close();
            }
        }

        private void Owner_LocationChanged(object sender, EventArgs e)
        {
            UpdateLocation();
        }

        #endregion


        #region Method

        public void SetMessage(string msg)
        {
            lab_Message.Text = msg;
        }

        public void SetConfirmText(string txt)
        {
            btn_Confirm._Text = txt;
        }

        public void SetCancelText(string txt)
        {
            btn_Cancel._Text = txt;
        }

        public void SetButtons(MessageBoxButtons buttons)
        {
            switch (buttons)
            {
                case MessageBoxButtons.OK:
                    btn_Confirm.Show();
                    btn_Cancel.Hide();
                    break;
                case MessageBoxButtons.OKCancel:
                    btn_Confirm.Show();
                    btn_Cancel.Show();
                    break;
                default:
                    btn_Confirm.Show();
                    btn_Cancel.Hide();
                    break;
            }

            btn_Cancel.Width = this.Width / 2;
        }

        public void Show(IWin32Window owner, MessageBoxResultHandler result)
        {
            messageboxResult = result;
            this.Show(owner);
            if (this.Owner != null)
            {
                UpdateLocation();
                this.Owner.LocationChanged += Owner_LocationChanged;
            }
        }

        private void UpdateLocation()
        {
            newLocation = this.Owner.PointToScreen(Point.Empty);
            newLocation.X += (this.Owner.Width - this.Width) / 2;
            newLocation.Y += (this.Owner.Height - this.Height) / 2;
            this.Location = newLocation;
        }

        #endregion

    }
}
